import path from 'path'
import env from './env' // 环境配置文件

export default {

  env: {
    baseUrl: env[process.env.NODE_ENV].BASE_API || 'http://localhost:3000'
  },

  server: {
    port: 3000,         // default: 3000
    host: '0.0.0.0'     // default: localhost
  },

  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      // 该meta标签的作用是让当前布局视口的宽度等于设备的宽度，同时不允许用户手动缩放
      { name: 'viewport', content: 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no' },

      // 默认http转成https协议
      { 'http-equiv': 'Content-Security-Policy', content: 'upgrade-insecure-requests' }, 
      
      // 安全协议
      // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
      // { 'http-equiv': 'X-Frame-Options', content: 'SAMEORIGIN' }, 
    ],
    
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Roboto' }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
    'element-ui/lib/theme-chalk/index.css',
  ],

  router: {
    /**
     *  先关闭中间件路由方案（时间：2021年12月14日22:37:46）
     * 
     */
    // 在每页渲染前运行中间件的逻辑
    // middleware: ['middleware']
  },

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    { src: '@/plugins/element-ui'},
    { src: '@/plugins/axios' },
    { src: '@/plugins/svg'},
    { src: '@/plugins/vuedraggable'},
    { src: '@/plugins/mavonEditor'},
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    '@nuxtjs/proxy',
  ],

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    proxy: true, // 表示开启代理
  },

  proxy: {
    '/api_v1': {
      target: env[process.env.NODE_ENV].BASE_API, 
      pathRewrite: {
        '^/api_v1': '/api_v1',
      },
      changeOrigin: true
    },
    '/baidu_api': {
      target: 'https://www.baidu.com', 
      pathRewrite: {
        '^/baidu_api': '/',
      },
      changeOrigin: true
    },
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: [/^element-ui/],

    extend(config, { isDev, isClient }) {
      const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
      svgRule.exclude = [path.resolve(__dirname, 'assets/svg')]

      // Includes /assets/icons/svg for svg-sprite-loader
      config.module.rules.push({
        test: /\.svg$/,
        include: [path.resolve(__dirname, 'assets/svg')],
        loader: 'svg-sprite-loader',
        options: {
          symbolId: 'icon-[name]',
        },
      })
    },
  }
}
